﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameAnimation : MonoBehaviour {

	public List<Sprite> animationList;
	public List<float> animateTimeList;
	private int animationIndex = 0;
	private bool playAnimation = false;
	private float playTime;
	//public delegate void AnimationEnd ();
	//public static event AnimationEnd animateEnd;
	void Update () {
		if (playAnimation) {
			if (playTime < animateTimeList [animationIndex]) {
				playTime += Time.deltaTime;
			}
			if ((animationIndex < animateTimeList.Count - 1) && playTime >= animateTimeList [animationIndex]) {
				animationIndex += 1;
				GetComponent<SpriteRenderer> ().sprite = animationList [animationIndex];
				playTime = 0;
			} else if (animationIndex >= animateTimeList.Count - 1) {
				PlayEnd ();
				animationIndex = animateTimeList.Count - 1;
				playAnimation = false;
			}
		}
	}

	public void Play(){
		animationIndex = 0;
		playAnimation = true;
		playTime = 0;
		GetComponent<SpriteRenderer> ().sprite = animationList [animationIndex];
	}

	public void PlayEnd(){
		if (GetComponent<Bubble> () != null) {
			GetComponent<Bubble> ().finished ();
		}
	}

	public float getPlayTime(){
		float time = 0;
		for(var i=0; i<animateTimeList.Count; i++){
			time += animateTimeList [i];
		}
		return time;
	}
}
